//
//  UIButton+UIButtonHelper.m
//  LocalEarth
//
//  Created by haoyi on 14-7-26.
//  Copyright (c) 2014年 Hooy1. All rights reserved.
//

#import "UIButton+UIButtonHelper.h"

@implementation UIButton (UIButtonHelper)

-(void)setImageAndTitleFrameWith:(UIButtonImageFrameType)Type{

    CGPoint buttonBoundsCenter = CGPointMake(CGRectGetMidX(self.bounds),
                                             CGRectGetMidY(self.bounds));
    // 找出imageView最终的center
    CGPoint endImageViewCenter = CGPointMake(buttonBoundsCenter.x,
                                             CGRectGetMidY(self.imageView.bounds));
    
    CGPoint endTitleLabelCenter = CGPointMake(buttonBoundsCenter.x,
                                              CGRectGetHeight(self.bounds) - CGRectGetMidY(self.titleLabel.bounds));
    // 取得imageView最初的center
    CGPoint startImageViewCenter = self.imageView.center;
    // 取得titleLabel最初的center
    CGPoint startTitleLabelCenter = self.titleLabel.center;
    
    // 设置imageEdgeInsets
    CGFloat imageEdgeInsetsTop = endImageViewCenter.y - startImageViewCenter.y + 5;
    CGFloat imageEdgeInsetsLeft = endImageViewCenter.x - startImageViewCenter.x;
    CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop;
    CGFloat imageEdgeInsetsRight = - imageEdgeInsetsLeft;
    self.imageEdgeInsets = UIEdgeInsetsMake(imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight);
    
    // 设置titleEdgeInsets
    CGFloat titleEdgeInsetsTop = endTitleLabelCenter.y-startTitleLabelCenter.y - 5;
    CGFloat titleEdgeInsetsLeft = endTitleLabelCenter.x - startTitleLabelCenter.x;
    CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop;
    CGFloat titleEdgeInsetsRight = -titleEdgeInsetsLeft;
    self.titleEdgeInsets = UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}

@end
